package com.jdz.gxq.info.admin.project.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

import com.jdz.gxq.info.admin.project.bo.Inspection;
import com.jdz.gxq.info.admin.project.bo.Project;
import com.jdz.gxq.info.admin.project.bo.YearInspection;

public class InspectionServiceImpl implements InspectionService {
	
	private QueryDAO queryDAO = null;
	
	private UpdateDAO updateDAO = null;
	
	/**
	 * InspectionProject检索分页用，查询记录条数
	 * @return
	 */
	public int getInspectionProjectsCount(Map<String, Object> params) {
		
		int totalCount = queryDAO.executeForObject("inspection.getInspectionProjectsCount",
				params, Integer.class);
		
		return totalCount;
	}
	
	/**
	 * InspectionProject检索
	 * @return
	 */
	public List<Project> getInspectionProjects(Map<String, Object> params) {
		
		return queryDAO.executeForObjectList("inspection.getInspectionProjects", params);
	}
	
	/**
	 * 得到Project信息
	 * @return
	 */
	public Project getProject(int projectId) {
		
		return queryDAO.executeForObject("inspection.getProjectById", projectId, Project.class);
	}
	
	/**
	 * 得到督查信息List
	 * @param projectId
	 */
	public List<YearInspection> getInspectionList(int projectId) {
		
		List<YearInspection> yearInspectList = queryDAO.executeForObjectList(
				"inspection.getYearInspections", projectId);

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("projectId", projectId);
		
		for (YearInspection yearInspection : yearInspectList) {

			params.put("year", yearInspection.getId());
			
			List<Inspection> inspectionList = queryDAO.executeForObjectList(
					"inspection.getMonthInspections", params);

			yearInspection.setInspectionList(inspectionList);
		}
		
		return yearInspectList;
	}
	
	public void insertYearInspection(YearInspection yearInspection) {
		updateDAO.execute("inspection.uinsertYearInspection", yearInspection);
	}
	
	/**
	 * 得到督查信息
	 * @param id
	 */
	public YearInspection getYearInspection(int id) {
		
		return queryDAO.executeForObject("inspection.getYearInspection", id, YearInspection.class);
	}
	
	/**
	 * 根据年度，得到督查信息
	 * @param year
	 * @return
	 */
	public YearInspection getYearInspectionByYear(String year) {
		return queryDAO.executeForObject("inspection.getYearInspectionByYear", year, YearInspection.class);
	}
	
	/**
	 * 更新年度督查信息
	 * @param yearInspection
	 */
	public void updateYearInspection(YearInspection yearInspection) {
		updateDAO.execute("inspection.updateYearInspection", yearInspection);
	}
	
	/**
	 * 删除年督查信息和所属的月督查信息
	 * @param id
	 */
	public void deleteYearInspection(int id) {
		
		updateDAO.execute("inspection.deleteYearInspection", id);
		updateDAO.execute("inspection.deleteInspectionByYear", id);
	}
	
	
	/**
	 * 追加月督查信息
	 * @param inspection
	 */
	public void insertInspection(Inspection inspection) {
		updateDAO.execute("inspection.insertInspection", inspection);
	}
	
	/**
	 * 根据id，得到月督查信息
	 * @param yearId
	 * @param month
	 * @return
	 */
	public Inspection getInspection(int id) {
		
		return queryDAO.executeForObject("inspection.getInspection", id, Inspection.class);
		
	}
	
	/**
	 * 根据年度id和月份，得到月督查信息
	 * @param yearId
	 * @param month
	 * @return
	 */
	public Inspection getInspectionByMonth(int yearId, String month) {
		
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("year", yearId);
		param.put("month", month);
		
		return queryDAO.executeForObject("inspection.getInspectionByMonth", param, Inspection.class);
		
	}
	
	/**
	 * 更新月督查信息
	 * @param inspection
	 */
	public void updateInspection(Inspection inspection) {
		updateDAO.execute("inspection.updateInspection", inspection);
	}
	
	/**
	 * 删除月督查信息
	 * @param id
	 */
	public void deleteInspection(int id) {
		updateDAO.execute("inspection.deleteInspection", id);
	}

	public void setQueryDAO(QueryDAO queryDAO) {
		this.queryDAO = queryDAO;
	}

	public void setUpdateDAO(UpdateDAO updateDAO) {
		this.updateDAO = updateDAO;
	}	
}
